package v1

import (
	"log"
	"net/http"
	"toy-car/models"
	"toy-car/pkg/e"
	"toy-car/pkg/util"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func AuthorityAdd(c *gin.Context) {
	var authority models.SysAuthority
	_ = c.ShouldBindJSON(&authority)

	log.Println(authority)

	data := make(map[string]interface{})
	code := e.SUCCESS
	if err, authBack := models.CreateAuthority(authority); err != nil {
		code = e.ERROR
		data["data"] = authBack
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": data,
	})
}

// @Tags Authority
// @Summary 更新角色信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysAuthority true "权限id, 权限名, 父角色id"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /authority/:id [put]
func AuthorityUpdate(c *gin.Context) {
	pid := com.StrTo(c.Param("id")).MustInt()
	var auth models.SysAuthority
	_ = c.ShouldBindJSON(&auth)

	data := make(map[string]interface{})
	code := e.SUCCESS
	if err, authority := models.UpdateAuthority(auth, pid); err != nil {
		code = e.ERROR
		data["data"] = authority
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": data,
	})
}

// @Tags Authority
// @Summary 分页获取角色列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.PageInfo true "页码, 每页大小"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /authority/getAuthorityList [get]
func GetAuthorityList(c *gin.Context) {
	var page, pageSize = util.GetPageAndSize(c)

	code := e.SUCCESS
	data := make(map[string]interface{})

	err, list, total := models.GetAuthorityInfoList(page, pageSize)
	if err != nil {
		code = e.ERROR
	}
	if code != e.SUCCESS {
		e.Fail(e.ERROR, nil, "", c)
		return
	}

	data["page"] = page
	data["pageSize"] = pageSize
	data["data"] = list
	data["total"] = total
	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": data,
	})
}

func GetAuthorityInfo(c *gin.Context) {
	id := com.StrTo(c.Param("id")).MustInt64()
	err, auth := models.GetAuthorityInfo(id)
	code := e.SUCCESS
	if err != nil {
		code = e.ERROR
	}
	if code != e.SUCCESS {
		e.Fail(e.ERROR, nil, "", c)
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": auth,
	})
}

// @Tags Authority
// @Summary 删除角色
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.PageInfo true "页码, 每页大小"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /authority/:id [delete]
func AuthorityDelete(c *gin.Context) {
	pid := com.StrTo(c.Param("id")).MustInt()
	var auth models.SysAuthority
	_ = c.ShouldBindJSON(&auth)

	data := make(map[string]interface{})
	code := e.SUCCESS
	if err := models.DeleteAuthority(&auth, pid); err != nil {
		code = e.ERROR
		data["data"] = auth
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMSG(code),
		"data": data,
	})
}
